//
//  FANKeyboardTools.swift
//  Weibo - Swift
//
//  Created by user on 16/4/26.
//  Copyright © 2016年 user. All rights reserved.
//

import UIKit

enum FANEmotionType: Int {
    case Recent = 0, Default, Emoji, Lxh
}


class FANKeyboardTools: UIStackView {
    
    var selectedSection:Int = 0{
        
        didSet{
            
            let button = self.viewWithTag(selectedSection) as! UIButton
            
            if  button != selectedButton{
                
                selectedButton?.selected = false
                
                button.selected = true
                
                selectedButton = button
            }
        }
    }
    
    var selectedButton:UIButton?
    
    var didToolsClickClosure:((type:FANEmotionType)->())?

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.distribution = .FillEqually
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
        self.tag = 20000
        
        addToolsButton("compose_emotion_table_left", title: "最近" ,type: .Recent)
        addToolsButton("compose_emotion_table_mid", title: "默认",type: .Default)
        addToolsButton("compose_emotion_table_mid", title: "Emoji", type: .Emoji)
        addToolsButton("compose_emotion_table_right", title: "浪小花",type: .Lxh)
    }
    
    func addToolsButton(imageName:String,title:String,type:FANEmotionType){
        
        let button = UIButton()
        
        button.setTitle(title, forState: UIControlState.Normal)
        
        button.tag = type.rawValue
        
        if button.tag == 0 {
            
            button.selected = true
            
            selectedButton = button
        }
        
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Selected)
        
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "\(imageName)_normal"), forState: UIControlState.Normal);
            
        button.setBackgroundImage(UIImage(named: "\(imageName)_selected"), forState: UIControlState.Selected)
        
        button.addTarget(self, action: "didClickButton:", forControlEvents: UIControlEvents.TouchUpInside);
        
        self.addArrangedSubview(button)
    }
    
    func didClickButton(button:UIButton){
        
        didToolsClickClosure!(type:FANEmotionType(rawValue: button.tag)!)
        
        selectedButton?.selected = false
        
        button.selected = true;
        
        selectedButton = button
        
        
    }
    


}
